package com.ornate.core.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.ContextLoader;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("unchecked")
public class ActionSupportProvider extends ActionSupport {

	// Properties
	private static final long serialVersionUID = 8379121036365548258L;

	private Object jsonObject;

	private static final String JSON_ROOT_PROPERTY = "jsonObject";

	protected static final String JSON = JSON_ROOT_PROPERTY, JSON_STRING = JSON_ROOT_PROPERTY, JSON_ARRAY = JSON_ROOT_PROPERTY, JSON_OBJECT = JSON_ROOT_PROPERTY, JSON_RESULT = JSON_ROOT_PROPERTY, JSON_EXCEPTION = JSON_ROOT_PROPERTY;

	// Methods
	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public ServletContext getApplication() {
		return getServletContext();
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	public String[] getParameterValues(String name) {
		return getRequest().getParameterValues(name);
	}

	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public HttpSession getSession(boolean create) {
		return getRequest().getSession(create);
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public String getRealPath(String path) {
		return getServletContext().getRealPath(path);
	}

	public Object getBean(String cls) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
	}

	public Object getBean(Class<?> cls) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
	}

	public Map<String, Object> getParameterMap() {
		Map<String, Object> params = new HashMap<String, Object>(getRequest().getParameterMap());
		Entry<String, Object> entry;
		String[] value;
		for (Iterator<Entry<String, Object>> iterator = params.entrySet().iterator(); iterator.hasNext();) {
			entry = iterator.next();
			value = ((String[]) entry.getValue());
			if (value.length == 1) {
				entry.setValue(value[0]);
				continue;
			}
			if (value.length == 0) {
				params.remove(entry.getKey());
				continue;
			}
			// ## FIXME DEBUG ##
			List<String> values = new ArrayList<String>();
			for (int i = 0, len = value.length; i < len; i++) {
				values.add(value[i]);
			}
			entry.setValue(values);
			// ## DEBUG ##
		}
		return params;
	}

	// Getters and Setters
	public Object getJSONObject() {
		return jsonObject;
	}

	public void setJSONObject(Object jsonObject) {
		this.jsonObject = jsonObject;
	}

	public void setJSONPagingResult(Object data, int total) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("data", data);
		result.put("total", total);
		this.jsonObject = result;
	}

	public void setJSONPagingResult(String dataProperty, Object data, String totalProperty, int total) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(dataProperty, data);
		result.put(totalProperty, total);
		this.jsonObject = result;
	}

	public void setJSONProcessResult(boolean success) {
		setJSONProcessResult(success, null);
	}

	public void setJSONProcessResult(boolean success, String message) {
		setJSONProcessResult(success, "message", message);
	}

	public void setJSONProcessResult(boolean success, String key, String value) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", success);
		if (key != null) result.put(key, value);
		this.jsonObject = result;
	}

	public void setJSONProcessResult(boolean success, int affected, String message) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", success);
		result.put("affected", affected);
		result.put("message", message);
		this.jsonObject = result;
	}

	public void setJSONProcessResult(Map<String, Object> result) {
		this.jsonObject = result;
	}

	/*public Map<String, Object> toMap(String key, Object value, Object... keysAndValues) {
		Map<String, Object> map = new HashMap<String, Object>(keysAndValues.length / 2);

		for (int i = 0; i < keysAndValues.length; i++) {
			map.put((String) keysAndValues[i], keysAndValues[i + 1]);
			i += 2;
		}
		
		return map;
	}*/

	public Map<String, Object> toMap(Object... keysAndValues) {
		Map<String, Object> map = new HashMap<String, Object>(keysAndValues.length / 2);

		for (int i = 0; i < keysAndValues.length; i += 2) {
			map.put((String) keysAndValues[i], keysAndValues[i + 1]);
		}

		return map;
	}

	public static void main(String[] args) {
		System.out.println(new ActionSupportProvider().toMap("account", "Alex", "password", 123456, "1", "xxx"));
	}

}
